package com.hxd.otc.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.flyco.dialog.widget.internal.InternalBasePopup;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.base.OnItemClickListener;
import com.hxd.otc.utils.ResourceUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/9/15 0015.
 * description ${钱包菜单}
 */
public class ExchangeSymbolPopupWindow extends BasePopupWindow<ExchangeSymbolPopupWindow> implements OnItemClickListener {

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private SymbolAdapter mAdapter;
    private ArrayList<String> mDatas;
    private String mSelItem;
    private OnSelSymbolListener mOnSelSymbolListener;
    private int mWidth;

    public ExchangeSymbolPopupWindow(Context context) {
        super(context);
    }

    @Override
    public View onCreatePopupView() {
        return View.inflate(mContext, getLayoutId(), null);
    }

    protected int getLayoutId() {
        return R.layout.popup_exchange_symbol;
    }

    @Override
    public void onViewCreated(View view) {
        super.onViewCreated(view);
        ButterKnife.bind(this, view);
        mRecyclerView.setLayoutManager(getLayoutManager());
        mAdapter = new SymbolAdapter(getContext());
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    public ExchangeSymbolPopupWindow setData(ArrayList<String> datas) {
        mDatas = datas;
        return this;
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    public ExchangeSymbolPopupWindow setSelItem(String selItem) {
        mSelItem = selItem;
        return this;
    }

    @Override
    public void setUiBeforShow() {
        mAdapter.setSelPosition(mSelItem);
        mAdapter.addAll(true, mDatas);
        ViewGroup.LayoutParams layoutParams = mRecyclerView.getLayoutParams();
        layoutParams.width = mWidth;
        mRecyclerView.setLayoutParams(layoutParams);
    }

    @Override
    public void onItemClick(int position) {
        if (mOnSelSymbolListener != null) {
            mOnSelSymbolListener.onSelSymbol(mAdapter.getItem(position));
            dismiss();
        }
    }

    public InternalBasePopup setWidth(int width) {
        mWidth = width;
        return this;
    }

    class SymbolAdapter extends BaseAdapter<FiatHolder, String> {
        private String mSelItem;

        SymbolAdapter(Context context) {
            super(context);
        }

        @Override
        protected void onBindHolder(@NonNull FiatHolder holder, int position) {
            String item = getItem(position);
            holder.tvTitle.setText(item);
            holder.tvTitle.setCompoundDrawables(null, null,
                    mSelItem != null && mSelItem.equals(item) ? ResourceUtil.getDrawable(R.mipmap.icon_exchange_checked) : null, null);
        }

        @NonNull
        @Override
        public FiatHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new FiatHolder(mInflater.inflate(R.layout.item_exchange_symbol, parent, false));
        }

        public void setSelPosition(String selItem) {
            mSelItem = selItem;
            notifyDataSetChanged();
        }
    }

    static class FiatHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;

        FiatHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public ExchangeSymbolPopupWindow setOnSelSymbolListener(OnSelSymbolListener onSelSymbolListener) {
        mOnSelSymbolListener = onSelSymbolListener;
        return this;
    }


    public interface OnSelSymbolListener {
        void onSelSymbol(String symbol);
    }


}
